package day03;

/**
 * @author Jetwang
 * @version 1.0
 * @since 2023/3/2 20:14
 * 力扣 234
 */
public class LinkedListPractice01 {

    public boolean isPalindrome(ListNode head) {
        if (head.next == null) return true;
        ListNode slow = head;
        ListNode fast = head;
        ListNode newList = null;
        ListNode oldList = head;
        // 没有走到链表结尾
        while(fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;

            // 前半个链表反转
            oldList.next = newList;
            newList = oldList;
            oldList = slow;
        }

        if (fast != null) {
            slow = slow.next;
        }

        while(newList != null) {
            if (newList.val != slow.val) {
                return false;
            }
            newList = newList.next;
            slow = slow.next;
        }
        return true;


        /*ListNode middleNode = slow;

        ListNode newList = null;
        while(middleNode != null) {
            ListNode p = middleNode.next;
            middleNode.next = newList;
            newList = middleNode;
            middleNode = p;
        }

        ListNode compareList = newList;
        while(compareList != null) {
            if (compareList.val != head.val) {
                return false;
            }
            head = head.next;
            compareList = compareList.next;
        }
        return true;
        */
    }


}
